<?php

namespace App\Controllers;

use CodeIgniter\Exceptions\PageNotFoundException;

class Media extends BaseController
{
    public function image($filename)
    {
        // 确保文件名安全，防止目录遍历攻击
        $filename = basename($filename);
        
        $path = WRITEPATH . 'data/' . $filename;
        
        // 检查文件是否存在
        if (!file_exists($path)) {
            throw PageNotFoundException::forPageNotFound();
        }
        
        // 获取文件类型
        $mime = mime_content_type($path);
        if (strpos($mime, 'image/') !== 0) {
            throw PageNotFoundException::forPageNotFound();
        }
        
        // 读取文件并输出
        return $this->response->setContentType($mime)
                              ->setBody(file_get_contents($path));
    }
}